import math
from datetime import datetime
from typing import Optional, List
from pydantic import BaseModel, Field
from schemas.customer import CustomerModel
from schemas.shopping_cart import ShoppingCartItem


class OrderAddress(BaseModel):
    id: int
    created_on: Optional[datetime] = None
    created_by: Optional[int] = None
    updated_on: Optional[datetime] = None
    updated_by: Optional[int] = None
    status: Optional[int] = 1
    recipient: str
    phone: str
    province: str
    city: Optional[str] = None
    district: Optional[str] = None
    details_address: Optional[str] = None


class OrderDeliveryPerson(BaseModel):
    name: str
    phone: str


class CreateOrderRequest(BaseModel):
    created_on: Optional[datetime] = None
    created_by: Optional[int] = None
    customer_id: Optional[int] = None
    customer_phone: str
    order_code: Optional[str] = Field(default_factory=lambda: str(math.floor(datetime.now().timestamp() * 1000)))
    items: List[ShoppingCartItem]
    delivery_method: int
    delivery_address: Optional[OrderAddress] = None
    delivery_date: Optional[datetime] = None
    presale: Optional[bool] = False
    presale_delivery_date: Optional[datetime] = None
    greeting_card: Optional[str] = None
    notes: Optional[str] = None
    subtotal: float
    total_price: float
    payment_status: Optional[int] = 1
    shipping_status: Optional[int] = 1


class OrderModel(BaseModel):
    id: int
    created_on: Optional[datetime] = None
    created_by: Optional[int] = None
    updated_on: Optional[datetime] = None
    updated_by: Optional[int] = None
    status: Optional[int] = 1
    customer_id: int
    customer_phone: str
    order_code: str
    items: List[ShoppingCartItem]
    delivery_method: int
    delivery_address: Optional[OrderAddress] = None
    delivery_date: Optional[datetime] = None
    delivery_person: Optional[OrderDeliveryPerson] = None
    presale: bool
    presale_delivery_date: Optional[datetime] = None
    greeting_card: Optional[str] = None
    notes: Optional[str] = None
    subtotal: float
    total_price: float
    payment_date: Optional[datetime] = None
    payment_status: Optional[int] = 1
    shipping_status: Optional[int] = 1

    customer: Optional[CustomerModel] = None

    class Config:
        from_attributes = True


class OrderPaginationRequest(BaseModel):
    order_status: int
    page: int
    page_size: int
    kw: Optional[str] = None
    presale: Optional[bool] = None
    flower: Optional[List[str]] = None


class OrderPagination(BaseModel):
    page: int
    page_size: int
    total: int
    data: List[OrderModel]


class OrderDeliveryRequest(BaseModel):
    order_id: int
    delivery_person_id: int
